/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ronsicecream.pos.master.processdetail;

import com.buanalintas.bynga.Action;
import com.buanalintas.bynga.Controller;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Page;
import com.buanalintas.bynga.Persistence;
import com.buanalintas.bynga.Utility;
import com.ronsicecream.pos.entities.PosMasterItem;
import com.ronsicecream.pos.entities.PosMasterProcess;
import com.ronsicecream.pos.entities.PosMasterProcessDetail;
import com.ronsicecream.pos.entities.PosMasterUnit;
import java.math.BigDecimal;
import javax.persistence.EntityManager;

/**
 *
 * @author user
 */
public class ProcessDetail extends Page {

    private Field processDetailId;
    private Field processDetailProcessId;
    private Field processDetailItemId;
    private Field processDetailQuantity;
    private Field processDetailPrice;
    private Field processDetailTotal;

    public ProcessDetail() {
        super();

        setTheClass(PosMasterProcessDetail.class);


        processDetailId = new Field();
        processDetailId.setName("processDetailId");
        processDetailId.setCaption(" id");
        //opnameId.setInfo("opname id");
        processDetailId.setListProperties(" align=\"left\" width=\"1%\" nowrap");
        processDetailId.setInputProperties(" size=\"20\"");
        getFields().add(processDetailId);

        processDetailProcessId = new Field();
        processDetailProcessId.setName("processDetailProcessId");
        processDetailProcessId.setCaption("process");
        processDetailProcessId.setRequired(true);
        processDetailProcessId.setListProperties(" align=\"left\" width=\"15%\" nowrap");
        processDetailProcessId.setInputProperties(" size=\"30\" ");
        getFields().add(processDetailProcessId);

        processDetailItemId = new Field();
        processDetailItemId.setName("processDetailItemId");
        processDetailItemId.setCaption("item");
        processDetailItemId.setRequired(true);
        processDetailItemId.setListProperties(" align=\"left\" width=\"15%\" nowrap");
        processDetailItemId.setInputProperties(" size=\"30\" ");
        processDetailItemId.getSelectorColumns().add(new String[]{"itemName", " Nama", "width=\"40%\""});
        processDetailItemId.getSelectorColumns().add(new String[]{"itemCode", " Code", "width=\"10%\" nowrap"});
        processDetailItemId.getSelectorColumns().add(new String[]{"itemTypeId", " Type", "width=\"20%\""});
        processDetailItemId.getSelectorColumns().add(new String[]{"itemMinimum", " Minimum", "width=\"10%\""});
        processDetailItemId.getSelectorColumns().add(new String[]{"itemSatuanId", " Satuan", "width=\"10%\""});
        processDetailItemId.getSelectorColumns().add(new String[]{"itemRasaId", " Rasa", "width=\"10%\""});
        getFields().add(processDetailItemId);

        processDetailQuantity = new Field();
        processDetailQuantity.setName("processDetailQuantity");
        processDetailQuantity.setCaption("quantity");
        processDetailQuantity.setRequired(true);
        processDetailQuantity.setListProperties(" align=\"center\" width=\"5%\" nowrap");
        processDetailQuantity.setInputProperties(" size=\"8\"");
        getFields().add(processDetailQuantity);

        processDetailPrice = new Field();
        processDetailPrice.setName("processDetailPrice");
        processDetailPrice.setCaption("price");
        processDetailPrice.setRequired(true);
        processDetailPrice.setListProperties(" align=\"left\" width=\"10%\" nowrap");
        processDetailPrice.setInputProperties(" size=\"20\"");
        processDetailPrice.setDefaultValue(0);
        getFields().add(processDetailPrice);

        processDetailTotal = new Field();
        processDetailTotal.setName("processDetailTotal");
        processDetailTotal.setCaption(" total");
        //opnameTimestamp.setInfo("opname timestamp");
        processDetailTotal.setListProperties(" align=\"center\" width=\"6%\" nowrap");
        processDetailTotal.setInputProperties(" size=\"12\"");
        //opnameTimestamp.setFormatter(DateFormatter.getInstanceDateTime(this));
        getFields().add(processDetailTotal);

        getActions().add(Action.TRANS_DETAIL_LIST);
        getActions().add(Action.TRANS_DETAIL_ADD);
        getActions().add(Action.TRANS_DETAIL_EDIT);
        fillFields();

        getFieldsAdd().clear();
        getFieldsAdd().add(processDetailItemId);
        getFieldsAdd().add(processDetailQuantity);
//        getFieldsAdd().add(processDetailPrice);

        getFieldsEdit().clear();
        getFieldsEdit().add(processDetailItemId);
//        getFieldsEdit().add(processDetailPrice);
        getFieldsEdit().add(processDetailQuantity);

        getFieldsList().clear();
        getFieldsList().add(processDetailItemId);
        getFieldsList().add(processDetailQuantity);
//        getFieldsList().add(processDetailPrice);
//        getFieldsList().add(processDetailTotal);

        getFieldsView().clear();
        getFieldsView().add(processDetailItemId);
        getFieldsView().add(processDetailQuantity);
//        getFieldsView().add(processDetailPrice);
//        getFieldsView().add(processDetailTotal); 
        setController(new ProcessDetailController(this));
    }

    class ProcessDetailController extends Controller {

        public ProcessDetailController(Page page) {
            super(page);
//            setOrder(processName);
//            setOrderType("DESC");
        }
//        @Override
//        public void beforeInsertDetail(Object data) {
//            PosMasterProcessDetail process = (PosMasterProcessDetail) data;
//                EntityManager em = getEntityManager();
//                PosMasterItem item= process.getProcessDetailItemId();
//                int type;
//                int unitType;
//                if (item.getItemTypeId().toString().equals("6")){
//                    type=6;
//                }
//            PosMasterUnit unit = (PosMasterUnit) em.createQuery("SELECT t PosMasterUnit t WHERE "
//                    + "t.unitUnitId.unitId="+ getCurrentUnit()).setHint(Persistence.REFRESH_HINT, true).getSingleResult();
//            if (unit.getUnitUnitTypeId().toString().equals("6")){
//                unitType=6;
//            }else if(unit.getUnitUnitTypeId().toString().equals("6")){
//                unitType=7;
//            }
//            PosMasterUnit regional = (PosMasterUnit) em.createQuery("SELECT t PosMasterUnit t WHERE "
//                    + "t.unitRegionalId.regionalId="+ unit.getUnitRegionalId()+ 
//                    "AND t.unitUnitTypeId.unitTypeId=6").setHint(Persistence.REFRESH_HINT, true).getSingleResult();
//            
//            BigDecimal total = BigDecimal.ZERO;
//            
//            total = process.getProcessDetailPrice().multiply(process.getProcessDetailQuantity());
//            process.setProcessDetailTotal(total);
//        }
    }
}
